package com.mwyq.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.mwyq.model.News;
import com.mwyq.service.NewsService;

@RestController
@RequestMapping("news")
public class NewsController {
	@Autowired
	private NewsService newsService;
	
	/**
	 * 显示新闻列表
	 * **/
	@RequestMapping(value = "/",method=RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request){
		
		/*
		 * 首页模块
		 * 1、新闻列表
		 * 2、对应新闻内容
		 * 3、.....
		 * */
		ModelAndView view = new ModelAndView("/WEB-INF/latestNews/latestNews");
		
		/**
		 * 得到所有新闻，以时间排序
		 * */
		allNews(view);
		
		
		return view;
	}
	
	public void allNews(ModelAndView view) {
//		List<News> latestNews =  newsService.getAllNews();
		List<News> latestNews = newsService.getLatestNews();
		
		view.addObject("latestNews",latestNews);
	}
	
	//每条新闻对应的新闻内容
	@RequestMapping(value = "/{id}/newsContent", method=RequestMethod.GET)
	public ModelAndView listtopNew(HttpServletRequest request,@PathVariable("id") int id){
		
		System.out.println("newsid:"+id);
		ModelAndView view = new ModelAndView("/WEB-INF/latestNews/newsContent");
		List<News> newsContent = newsService.getNewsContent(id);
//		System.out.println(newsContent);
		view.addObject("newsContent",newsContent);
		
		return view;
	}
}
